package com.yanqu.road.entity.activity.stronghold;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@FieldTypeAnnotation(tableName = "t_c_activity_stronghold_wild_user")
public class StrongholdWildUserData extends DataObject {
    @FieldTypeAnnotation(select = true, update = true)
    private int activityId;
    @FieldTypeAnnotation(update = true)
    private long userId;
    @FieldTypeAnnotation(update = true)
    private long serverId;
    @FieldTypeAnnotation
    private int wildId;
    @FieldTypeAnnotation
    private UserPatrons patrons;
    @FieldTypeAnnotation
    private int rewardPoints;
    @FieldTypeAnnotation
    private UserBaseInfo userBaseInfo;
    @FieldTypeAnnotation
    private List<Integer> boxList = new ArrayList<>();
    @FieldTypeAnnotation
    private StrongholdBaseEvent eventData;
    @FieldTypeAnnotation
    private Map<Long, StrongholdDefendInfo> defendInfo = new ConcurrentHashMap<>();

    public int getActivityId() {
        return activityId;
    }

    public long getUserId() {
        return userId;
    }

    public long getServerId() {
        return serverId;
    }

    public int getWildId() {
        return wildId;
    }

    public UserPatrons getPatrons() {
        return patrons;
    }

    public int getRewardPoints() {
        return rewardPoints;
    }

    public UserBaseInfo getUserBaseInfo() {
        return userBaseInfo;
    }

    public List<Integer> getBoxList() {
        return boxList;
    }

    public StrongholdBaseEvent getEventData() {
        return eventData;
    }

    public Map<Long, StrongholdDefendInfo> getDefendInfo() {
        return defendInfo;
    }

    public void setDefendInfo(Map<Long, StrongholdDefendInfo> defendInfo) {
        this.defendInfo = defendInfo;
        setUpdateOption();
    }

    public void resetDefenInfo() {
        this.defendInfo.clear();
        setUpdateOption();
    }

    public void addDefendInfo(long userId) {
        if (defendInfo.containsKey(userId)) {
            StrongholdDefendInfo info = defendInfo.get(userId);
            info.setTimes(info.getTimes() + 1);
        } else {
            StrongholdDefendInfo info = new StrongholdDefendInfo();
            info.setUserId(userId);
            info.setTimes(1);
            defendInfo.put(userId, info);
        }
    }

    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }

    }

    public void setUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            setOp(DataStatus.Update);
        }

    }

    public void setServerId(long serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            setOp(DataStatus.Update);
        }

    }

    public void setWildId(int wildId) {
        if (this.wildId != wildId) {
            this.wildId = wildId;
            setOp(DataStatus.Update);
        }

    }

    public void setRewardPoints(int rewardPoints) {
        if (this.rewardPoints != rewardPoints) {
            this.rewardPoints = rewardPoints;
            setOp(DataStatus.Update);
        }
    }

    public void setPatrons(UserPatrons patrons) {
        this.patrons = patrons;
    }

    public void setUserBaseInfo(UserBaseInfo userBaseInfo) {
        this.userBaseInfo = userBaseInfo;
    }

    public void setBoxList(List<Integer> boxList) {
        this.boxList = boxList;
        setUpdateOption();
    }

    public void setEventData(StrongholdBaseEvent eventData) {
        this.eventData = eventData;
        setUpdateOption();
    }


    public int addBox(Integer boxId) {
        int index = -1;
        for (int i = 0; i < boxList.size(); i++) {
            if (boxList.get(i) == 0) {
                index = i;
                break;
            }
        }
        if (index > -1) {
            boxList.remove(index);
            boxList.add(index, boxId);
        } else {
            boxList.add(boxId);
        }
        setUpdateOption();
        return index;
    }

    public void removeBox(int boxId) {
        int index = -1;
        for (int i = 0; i < boxList.size(); i++) {
            int id = boxList.get(i);
            if (id == boxId) {
                index = i;
                break;
            }
        }
        if (index > -1) {
            boxList.remove(index);
            boxList.add(index, 0);
        }
        setUpdateOption();
    }

    public void resetEventEmpty() {
        eventData = null;
        setUpdateOption();
    }

    public int getDefendTimes(long userId) {
        return defendInfo.get(userId).getTimes();
    }
}
